<template>
  <view class="page_diy_view page_class_information_view" :style="{ paddingTop: vuex_custom_bar_height + 'px' }">
    <tn-nav-bar>班级信息详情</tn-nav-bar>
    <view id="page_diy_view" class="page-from">
      <uni-forms :modelValue="form" v-if="is_view()" labelWidth="70px">

        <uni-forms-item v-if="$check_field('get','class_name') || ($check_field('add','class_name') || $check_field('set','class_name'))" label="班级名称" name="class_name">
                      <uni-easyinput type="text" v-model="form['class_name']" v-if="(form['class_information_id'] && $check_field('set','class_name')) || (!form['class_information_id'] && $check_field('add','class_name'))" :disabled="disabledObj['class_name_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','class_name')">
            {{ form['class_name'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','class_major') || ($check_field('add','class_major') || $check_field('set','class_major'))" label="班级专业" name="class_major">
                      <uni-easyinput type="text" v-model="form['class_major']" v-if="(form['class_information_id'] && $check_field('set','class_major')) || (!form['class_information_id'] && $check_field('add','class_major'))" :disabled="disabledObj['class_major_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','class_major')">
            {{ form['class_major'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','class_director') || ($check_field('add','class_director') || $check_field('set','class_director'))" label="班级主任" name="class_director">
              <uni-data-select
              id="form_class_director"
              v-model="form['class_director']"
              :localdata="list_user_class_director"
              :clear="!disabledObj['class_director_isDisabled']"
              :disabled="disabledObj['class_director_isDisabled']"
              v-if="(form['class_information_id'] && $check_field('set', 'class_director')) || (!form['class_information_id'] && $check_field('add', 'class_director'))"
			   style="width: 100%;"
          ></uni-data-select>
          <uni-data-select
              v-model="form['class_director']"
              :localdata="list_user_class_director"
              :clear="false"
              :disabled="true"
              v-else-if="$check_field('get', 'class_director')"
              id="class_director"
			  style="width: 100%;"
          ></uni-data-select>
            </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','teachers_name') || ($check_field('add','teachers_name') || $check_field('set','teachers_name'))" label="教师姓名" name="teachers_name">
                      <uni-easyinput type="text" v-model="form['teachers_name']" v-if="(form['class_information_id'] && $check_field('set','teachers_name')) || (!form['class_information_id'] && $check_field('add','teachers_name'))" :disabled="disabledObj['teachers_name_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','teachers_name')">
            {{ form['teachers_name'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','class_size') || ($check_field('add','class_size') || $check_field('set','class_size'))" label="班级人数" name="class_size">
                      <uni-easyinput type="text" v-model="form['class_size']" v-if="(form['class_information_id'] && $check_field('set','class_size')) || (!form['class_information_id'] && $check_field('add','class_size'))" :disabled="disabledObj['class_size_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','class_size')">
            {{ form['class_size'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','classroom_location') || ($check_field('add','classroom_location') || $check_field('set','classroom_location'))" label="教室位置" name="classroom_location">
                      <uni-easyinput type="text" v-model="form['classroom_location']" v-if="(form['class_information_id'] && $check_field('set','classroom_location')) || (!form['class_information_id'] && $check_field('add','classroom_location'))" :disabled="disabledObj['classroom_location_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','classroom_location')">
            {{ form['classroom_location'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','class_details') || ($check_field('add','class_details') || $check_field('set','class_details'))" label="班级详情" name="class_details">
              <uni-easyinput type="textarea" v-model="form['class_details']" v-if="(form['class_information_id'] && $check_field('set', 'class_details')) || (!form['class_information_id'] && $check_field('add', 'class_details'))" :disabled="disabledObj['class_details_isDisabled']"/>
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get', 'class_details')">
            {{ form['class_details'] }}
          </text>
            </uni-forms-item>
      </uni-forms>
      <view class="form-footer" v-if="$check_action('/class_information/view','set') || ($check_action('/class_information/view','add') || $check_option('/class_information/table','examine'))">
        <view class="me-btn btn-submit" @click="submit_()"> 提交 </view>
        <view class="me-btn btn-cancel" @click="cancel()">取消</view>
      </view>
      <view class="form_button" v-else>
        <view class="me-btn btn-cancel" @click="cancel()">返回</view>
      </view>
    </view>
  </view>
</template>

<script>
import mixin from '@/libs/mixins/page.js';
                            import { uploadFilePathApi, uploadFileApi } from '@/api/common.js';
export default {
  mixins: [mixin],
  data() {
    return {
	  today: new Date().toISOString().split('T')[0],
      field: "class_information_id",
      url_add: "~/api/class_information/add?",
      url_set: "~/api/class_information/set?",
      url_get_obj: "~/api/class_information/get_obj?",
      url_upload: "~/api/class_information/upload?",
      class_name: null,
      class_major: null,
      class_director: null,
      teachers_name: null,
      class_size: null,
      classroom_location: null,
      class_details: null,
      query: {
        "class_information_id": 0,
      },
      form: {
          "class_name":  '', // 班级名称
            "class_major":  '', // 班级专业
            "class_director": 0, // 班级主任
            "teachers_name":  '', // 教师姓名
            "class_size":  0 , // 班级人数
            "classroom_location":  '', // 教室位置
            "class_details":  '', // 班级详情
          "class_information_id": 0, // ID
      },
      disabledObj:{
          "class_name_isDisabled": false,
            "class_major_isDisabled": false,
            "class_director_isDisabled": false,
            "teachers_name_isDisabled": false,
                "classroom_location_isDisabled": false,
            "class_details_isDisabled": false,
        },
                              // 用户列表
      list_user_class_director: [],
                                                };
  },
  methods: {
    /**
     * 提交前验证事件
     * @param {Object} 请求参数
     * @return {String} 验证成功返回null, 失败返回错误提示
     */
    async submit_check(param) {
                                                                                                                      return null;
    },
    changeLog(v, value) {
      this.form[value] = v;
    },
    uploadFile_(type) {
      // #ifdef APP-VUE
      uni.chooseFile({
        count: 1,
        success: (chooseImageRes) => {
          const tempFilePaths = chooseImageRes.tempFilePaths;
          this.successChoose(tempFilePaths[0], type);
        },
      });
      // #endif
      // #ifdef !APP-VUE
      if (
        uni.getSystemInfoSync().uniPlatform != 'mp-weixin' ||
        uni.getSystemInfoSync().platform == 'devtools'
      ) {
        uni.chooseImage({
          count: 1,
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths;
            this.successChoose(tempFilePaths[0], type);
          },
        });
      } else {
        wx.chooseMessageFile({
          count: 1,
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFiles;
            this.successChoose(tempFilePaths[0].path, type);
          },
        });
      }
      // #endif
    },
    successChoose(filePath, type) {
      uni.uploadFile({
        url: this.$fullUrl('~/api/class_information/upload?'), //仅为示例，非真实的接口地址
        filePath,
        name: 'file',
        success: (uploadFileRes) => {
          if (uploadFileRes.data.error) {
            uni.showToast({ title: uploadFileRes.data.error.message, icon: 'none' });
          } else {
            this[type] = JSON.parse(uploadFileRes.data).result.url;
          }
        },
      });
    },
    close_(type) {
      if (type == 'class_name') this['class_name'] = this.form['class_name'] = "";
      if (type == 'class_major') this['class_major'] = this.form['class_major'] = "";
      if (type == 'class_director') this['class_director'] = this.form['class_director'] = "";
      if (type == 'teachers_name') this['teachers_name'] = this.form['teachers_name'] = "";
      if (type == 'class_size') this['class_size'] = this.form['class_size'] = "";
      if (type == 'classroom_location') this['classroom_location'] = this.form['classroom_location'] = "";
      if (type == 'class_details') this['class_details'] = this.form['class_details'] = "";
    },
    submit_() {
      if (this['class_name'] !== null) this.form['class_name'] = this['class_name']
      if (this['class_major'] !== null) this.form['class_major'] = this['class_major']
      if (this['class_director'] !== null) this.form['class_director'] = this['class_director']
      if (this['teachers_name'] !== null) this.form['teachers_name'] = this['teachers_name']
      if (this['class_size'] !== null) this.form['class_size'] = this['class_size']
      if (this['classroom_location'] !== null) this.form['classroom_location'] = this['classroom_location']
      if (this['class_details'] !== null) this.form['class_details'] = this['class_details']
      console.log(this.form)
      this.submit();
    },
    /**
     * 上传文件
     * @param {Object} param文件参数
     */
    change_file(key_name) {
      var _self = this;
      // 选择图像方法
      uni.chooseFile({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (file) => {
          const tempFilePaths = file.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi(
            '/class_information/upload',
            filePath,
            undefined,
            {
              i_want_to_customize: 'test',
            },
            (task) => {
              task.onProgressUpdate((res) => {
                this.percent = res.progress;
                console.log('上传进度' + res.progress);
                console.log('已经上传的数据长度' + res.totalBytesSent);
                console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
              });
            }
          ).then((res) => {
            const filename = res.result.url;
            this.form[key_name] = filename;
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    /**
     * 上传图片
     * @param {Object} param文件参数
     */
    change_img(key_name) {
      var _self = this;
      _self.upload_img_flag = false;
      // 选择图像方法
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (file) => {
          const tempFilePaths = file.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi('/class_information/upload',filePath,undefined,undefined,
            (task) => {
              task.onProgressUpdate((res) => {
                this.percent = res.progress;
                console.log('上传进度' + res.progress);
                console.log('已经上传的数据长度' + res.totalBytesSent);
                console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
              });
            }
          ).then((res) => {
            const filename = res.result.url;
            this.form[key_name] = filename;
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    /**
     * 获取对象后获取缓存表单
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_before(param) {
      var form = uni.db.get('form');
      if (form) {
        delete form.examine_state;
        delete form.examine_reply;
        this.obj = uni.push(this.obj, form);
        this.form = uni.push(this.form, form);
      }
      var arr = [];
      for (let key in form) {
        arr.push(key);
      }
      for (var i = 0; i < arr.length; i++) {
        this.disabledObj[arr[i] + '_isDisabled'] = true;
      }
                                  uni.db.del('form');

      return param;
    },
    
      
          /**
     * 获取教师用户用户列表
     */
    async get_list_user_class_director() {
      var json = await this.$get("~/api/user/get_list?user_group=教师用户");
      if(json.result && json.result.list){
        json.result.list.map((o) => this.list_user_class_director.push({value:o.user_id,text:o.nickname + '-' + o.username}));
      }
      else if(json.error){
        console.error(json.error);
      }
    },
      
      
      
      
      
      /**
     * 获取对象之后
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_after(json, func) {
                                                                                        },

    is_view() {
      var bl = this.user_group == '管理员';

      if (!bl) {
        bl = this.$check_action('/class_information/table', 'add');
        console.log(bl ? '你有表格添加权限视作有添加权限' : '你没有表格添加权限');
      }
      if (!bl) {
        bl = this.$check_action('/class_information/table', 'set');
        console.log(bl ? '你有表格添加权限视作有修改权限' : '你没有表格修改权限');
      }
      if (!bl) {
        bl = this.$check_action('/class_information/view', 'add');
        console.log(bl ? '你有视图添加权限视作有添加权限' : '你没有视图添加权限');
      }
      if (!bl) {
        bl = this.$check_action('/class_information/view', 'set');
        console.log(bl ? '你有视图修改权限视作有修改权限' : '你没有视图修改权限');
      }
      if (!bl) {
        bl = this.$check_action('/class_information/view', 'get');
        console.log(bl ? '你有视图查询权限视作有查询权限' : '你没有视图查询权限');
      }

      console.log(
        bl
          ? '具有当前页面的查看权，请注意这不代表你有字段的查看权'
          : '无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行'
      );

      return bl;
    },
  },
  created() {
                this.get_list_user_class_director();
                          },
};
</script>

<style lang="scss" scoped>
  @import '@/styles/pagesC/index.scss';
</style>